﻿using System;
using System.Threading;
using System.Windows;
using Caliburn.Micro;
using System.ComponentModel.Composition;
using ChessKit.ViewModel;

namespace BoardControlTestStand
{

  [Export(typeof(IShell))]
  public class ShellViewModel : Screen
  {
    #region ' Board Property (change notification) '

    public BoardViewModel Board
    {
      get { return _board; }
      set
      {
        if (_board == value) return;
        _board = value;
        NotifyOfPropertyChange(() => Board);
      }
    }

    private BoardViewModel _board;

    #endregion
    public ShellViewModel()
    {
      Board = new BoardViewModel();
    }

    public void Move()
    {
      Board.RandomMove();
    }

    public void CopyFen()
    {
      Board.CopyFen();
    }

    public void LoadFen()
    {
      var testBoard = new BoardViewModel();
      testBoard.LoadFen(GetClipboardText());
      Board = testBoard;
    }
    public string GetClipboardText()
    {
      for (var i = 0; i < 3; i++)
      {
        try
        {
          return Clipboard.GetText();
        }
        catch (FormatException e)
        {
          return null;
        }
        catch (Exception e)
        {
          Console.WriteLine(e);
        }
        Thread.Sleep(0);
      }
      return null;
    }

    public void UndoMove()
    {
      Board.Undo();
    }
  }
}
